package weather

import (
	"errors"
	"io/ioutil"
	"net/http"
	"strings"
)

type WeatherApi struct {
	status_code string
}

func WeatherApi_GetWeather(location string) (string, error) {
	var url string
	url = "https://api.seniverse.com/v3/weather/daily.json?key=" + ConfigCell.WaetherApiKey + "&location=" + location + "&language=zh-Hans&unit=c&start=0&days=3"

	resp, err := http.Get(url)
	if err != nil {
		return "", err
	}

	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return "", err
	}

	// 判断状态 有没有resoult
	context := string(body)
	if strings.Contains(context, "{\"results\":") {
		return context, nil
	}

	// 不包含
	LogFile.Println("天气接口调用出错-获取天气")
	return "nil", errors.New(context)
}

func WeatherApi_GetLocation(IP string) (string, error) {
	var url string
	url = "https://api.seniverse.com/v3/location/search.json?key=" + ConfigCell.WaetherApiKey + "&q=" + IP + "&language=en"
	resp, err := http.Get(url)
	if err != nil {
		return "", err
	}

	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return "", err
	}

	// 判断状态 有没有resoult
	context := string(body)
	if strings.Contains(context, "{\"results\":") {
		return context, nil
	}

	// 不包含
	LogFile.Println("天气接口调用出错-获取地址")
	return "nil", errors.New(context)
}
